﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YiLe.BLL.Service;
using YiLe.Model.Domain;
using Spring.Context;

namespace YiLe.View.Roles
{
    public partial class RoleListForm : Form
    {
        private IRoleService roleService;
        private IPermissionService permissionService;

        public RoleListForm(Spring.Context.IApplicationContext ctx)
        {
            InitializeComponent();

            roleService = ctx["RoleService"] as IRoleService;
            permissionService = ctx["PermissionService"] as IPermissionService;
        }

        /// <summary>
        /// 角色列表读取事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void RoleListForm_Load(object sender, EventArgs e)
        {
            IList<Role> roleList = roleService.GetAll();
            this.bsList.DataSource = roleList;
        }

        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string rolename = this.tbRoleName.Text.Trim();
            var role = new Role(){RoleName=rolename};
            IList<Role> roleList = roleService.Search(role);
            this.bsList.DataSource = roleList;
        }

        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void add_Click(object sender, EventArgs e)
        {
            IApplicationContext ctx = (this.MdiParent as MainForm).ApplicationContext;
            RoleSaveForm saveForm = new RoleSaveForm(ctx, this);
            saveForm.WindowState = FormWindowState.Normal;
            saveForm.StartPosition = FormStartPosition.CenterScreen;
            saveForm.TopMost = true;
            this.TopLevel = false;
            saveForm.ShowDialog(this.MdiParent);
        }

        /// <summary>
        /// 关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 修改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void modify_Click(object sender, EventArgs e)
        {
            if (bsList.Current == null)
            {
                MessageBox.Show("请先选择要修改的记录");
                return;
            }
            else
            {
                Role role = bsList.Current as Role;
                IApplicationContext ctx = (this.MdiParent as MainForm).ApplicationContext;

                RoleSaveForm saveForm = new RoleSaveForm(ctx, this, role);
                saveForm.WindowState = FormWindowState.Normal;
                saveForm.StartPosition = FormStartPosition.CenterScreen;
                saveForm.TopMost = true;
                this.TopLevel = false;
                saveForm.ShowDialog(this.MdiParent);
            }

        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void delete_Click(object sender, EventArgs e)
        {
            if (this.dgvList.SelectedRows.Count == 0)
            {
                MessageBox.Show("请先选择要删除的记录");
                return;
            }
            if (MessageBox.Show("是否要删除该记录", "确定", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                foreach (DataGridViewRow row in this.dgvList.SelectedRows)
                {
                    Role role = row.DataBoundItem as Role;
                    if (role.Users.Count != 0)
                    {
                        MessageBox.Show("该角色还有用户使用，请先删除用户再删除该角色");
                    }
                    else
                    {
                        roleService.Delete(role);
                    }
                }
                this.RoleListForm_Load(sender, e);//刷新list
            }
        }

        private void dgvList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {

                //获取控件的值
                if (Boolean.Parse(this.dgvList.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()))
                {
                    this.dgvList.Rows[e.RowIndex].Cells[0].Value = 0;
                    this.dgvList.Rows[e.RowIndex].Selected = false;
                }
                else
                {
                    this.dgvList.Rows[e.RowIndex].Cells[0].Value = 1;
                    this.dgvList.Rows[e.RowIndex].Selected = true;
                }
            }
        }
    }
}
